package com.spy.system.controller;

import com.spy.common.core.domain.R;
import com.spy.system.domain.SysSupplierDelivery;
import com.spy.system.service.ISysSupplierDeliveryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.spy.common.annotation.Log;
import com.spy.common.core.controller.BaseController;
import com.spy.common.core.domain.AjaxResult;
import com.spy.common.enums.BusinessType;
import com.spy.system.domain.SysSupplier;
import com.spy.system.service.ISysSupplierService;
import com.spy.common.utils.poi.ExcelUtil;
import com.spy.common.core.page.TableDataInfo;

/**
 * 供应商基本信息Controller
 *
 * @author ruoyi
 * @date 2025-11-09
 */
@RestController
@RequestMapping("/system/supplier")
@Api(tags = "供应商基本信息相关接口")
public class SysSupplierController extends BaseController
{
    @Autowired
    private ISysSupplierService sysSupplierService;
    @Autowired
    private ISysSupplierDeliveryService deliveryService;// 注入供货明细服务

/**
 * 查询供应商基本信息列表
 */
@PreAuthorize("@ss.hasPermi('system:supplier:list')")
@GetMapping("/list")
@ApiOperation("查询供应商基本信息列表")
    public TableDataInfo<SysSupplier> list(@ApiParam(value = "供应商基本信息查询条件") SysSupplier sysSupplier)
    {
        startPage();
        List<SysSupplier> list = sysSupplierService.selectSysSupplierList(sysSupplier);
        return getDataTable(list);
    }
    @GetMapping("/delivery/list")
    public AjaxResult getDeliveryList(Long supplierId) {
        if (supplierId == null) {
            return AjaxResult.error("供应商ID不能为空");
        }
        List<SysSupplierDelivery> deliveryList = deliveryService.getDeliveryListBySupplierId(supplierId);
        return AjaxResult.success(deliveryList);
    }

    /**
     * 导出供应商基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:supplier:export')")
    @Log(title = "供应商基本信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出供应商基本信息列表")
    public void export(HttpServletResponse response, @ApiParam(value = "供应商基本信息查询条件") SysSupplier sysSupplier)
    {
        List<SysSupplier> list = sysSupplierService.selectSysSupplierList(sysSupplier);
        ExcelUtil<SysSupplier> util = new ExcelUtil<SysSupplier>(SysSupplier.class);
        util.exportExcel(response, list, "供应商基本信息数据");
    }

    /**
     * 获取供应商基本信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:supplier:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取供应商基本信息详细信息")
    public R<SysSupplier> getInfo(@ApiParam(value = "供应商基本信息ID", required = true)
                                   @PathVariable("id") Long id)
    {
                return R.ok(sysSupplierService.selectSysSupplierById(id));
    }

    /**
     * 新增供应商基本信息
     */
    @PreAuthorize("@ss.hasPermi('system:supplier:add')")
    @Log(title = "供应商基本信息", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增供应商基本信息")
    public AjaxResult add(@ApiParam(value = "供应商基本信息实体", required = true) @RequestBody SysSupplier sysSupplier)
    {
        return toAjax(sysSupplierService.insertSysSupplier(sysSupplier));
    }
    // 新增：供货占比统计接口
    @GetMapping("/delivery/ratio")
    public AjaxResult getDeliveryRatio(String timeRange) {
        if (timeRange == null) {
            timeRange = "近1月";
        }
        Map<String, BigDecimal> ratioMap = deliveryService.getSupplierDeliveryRatio(timeRange);
        return AjaxResult.success(ratioMap);

    }
    /**
     * 修改供应商基本信息
     */
    @PreAuthorize("@ss.hasPermi('system:supplier:edit')")
    @Log(title = "供应商基本信息", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改供应商基本信息")
    public AjaxResult edit(@ApiParam(value = "供应商基本信息实体", required = true)  @RequestBody SysSupplier sysSupplier)
    {
        return toAjax(sysSupplierService.updateSysSupplier(sysSupplier));
    }

    /**
     * 删除供应商基本信息
     */
    @PreAuthorize("@ss.hasPermi('system:supplier:remove')")
    @Log(title = "供应商基本信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除供应商基本信息")
    public AjaxResult remove(@ApiParam(value = "供应商基本信息ID数组", required = true) @PathVariable Long[] ids)
    {
        return toAjax(sysSupplierService.deleteSysSupplierByIds(ids));
    }
    // 新增：独立的排行接口（给供应商排行页面用）
    @GetMapping("/rankList")
    public TableDataInfo rankList(SysSupplier sysSupplier) {
        startPage(); // 支持分页（比如一页显示10个排行）
        // 调用排行专用方法（自带score倒序）
        List<SysSupplier> rankList = sysSupplierService.selectSysSupplierRankList(sysSupplier);
        return getDataTable(rankList);
    }
    @GetMapping("/score/list")
    public AjaxResult getSupplierScore() {
        List<Map<String, Object>> scoreList = sysSupplierService.calculateSupplierScore();
        return AjaxResult.success(scoreList);
    }
}